//
//  DeviceDataInfo.m
//  Yanwei
//
//  Created by iOS on 2018/2/26.
//  Copyright © 2018年 DCloud. All rights reserved.
//

#import "DeviceDataInfo.h"
#import "AFNetworkReachabilityManager.h"
//#import <CoreTelephony/CTTelephonyNetworkInfo.h>
//#import <CoreTelephony/CTCarrier.h>
//#import "PublickeyChain.h"
//#import <CoreLocation/CoreLocation.h>
//#import <EventKit/EventKit.h>

@interface DeviceDataInfo()
//<BMKLocationServiceDelegate,UIAlertViewDelegate>

/** 当前网络状态 */
@property (nonatomic, assign) YWNetWorkStatus status;
//@property (nonatomic, strong) BMKMapManager *mapManager;
//@property (nonatomic, strong) BMKLocationService *locService;
//@property (nonatomic, strong) EKEventStore *eventStore;
@end

@implementation DeviceDataInfo
//- (BMKMapManager *)mapManager {
//    if (!_mapManager) {
//        _mapManager = [[BMKMapManager alloc] init];
//    }
//    return _mapManager;
//}
//
//- (BMKLocationService *)locService {
//    if (!_locService) {
//        _locService = [[BMKLocationService alloc]init];
//        _locService.delegate = self;
//    }
//    return _locService;
//}
//
//- (EKEventStore *)eventStore {
//    if (!_eventStore) {
//        _eventStore = [[EKEventStore alloc] init];
//    }
//    return _eventStore;
//}
//
//+ (instancetype)shareDataInfo {
//    return _dataInfo;
//}
//
//static DeviceDataInfo *_dataInfo;
//+ (void)load {
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        _dataInfo = [[DeviceDataInfo alloc] init];
//        // 获取网络
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [[self shareDataInfo] afnReachability];
//        });
//        // 从钥匙串中取出经纬度
//        NSDictionary *userDict = (NSDictionary *)[PublickeyChain readKeyChain:UserLocationKey KeyChainKey:KeyChainAppKey];
//        if (!userDict) {
//            return;
//        }
//        _dataInfo.latitude = userDict[@"latitude"];
//        _dataInfo.longitude = userDict[@"longitude"];
//    });
//}
//
//#pragma mark - AFNetworkingReachability
//- (void)afnReachability {
//    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//        // 一共有四种状态
//        switch (status) {
//            case AFNetworkReachabilityStatusNotReachable:
//                NSLog(@"AFNetworkReachability Not Reachable");
//                self.status = YWNetWorkStatusNotReachable;
//                break;
//            case AFNetworkReachabilityStatusReachableViaWWAN:
//                NSLog(@"AFNetworkReachability Reachable via WWAN");
//                self.status = YWNetWorkStatusWiFi;
//                [self reachableViaWWAN];
//                break;
//            case AFNetworkReachabilityStatusReachableViaWiFi:
//                NSLog(@"AFNetworkReachability Reachable via WiFi");
//                self.status = YWNetWorkStatusWiFi;
//                break;
//            case AFNetworkReachabilityStatusUnknown:
//            default:
//                NSLog(@"AFNetworkReachability Unknown");
//                self.status = YWNetWorkStatusUnknown;
//                break;
//        }
//    }];
//    dispatch_async(dispatch_get_global_queue(0, 0), ^{
//        [[AFNetworkReachabilityManager sharedManager] startMonitoring];
//        [[NSRunLoop currentRunLoop] run];
//    });
//}
//
//+ (NSString *)currentNetWork {
//    DeviceDataInfo * dataInfo = [DeviceDataInfo shareDataInfo];
//    switch (dataInfo.status) {
//        case YWNetWorkStatusWiFi:
//            return @"1";
//        case YWNetWorkStatusWWAN2G:
//        case YWNetWorkStatusWWAN3G:
//            return @"3";
//        case YWNetWorkStatusWWAN4G:
//            return @"4";
//        default:
//            return nil;
//    }
//}
//
//+ (NSString *)telephonyCarrier{
//    CTTelephonyNetworkInfo * networkInfo = [[CTTelephonyNetworkInfo alloc]init];
//    CTCarrier * carrier = [networkInfo subscriberCellularProvider];
//    if (!carrier.isoCountryCode) {
//        return nil;
//    }
//    NSString *carrierName = [carrier carrierName];
//    if ([carrierName containsString:@"电信"]) {
//        return @"1";
//    } else if ([carrierName containsString:@"联通"]) {
//        return @"2";
//    }  else if ([carrierName containsString:@"移动"]) {
//        return @"3";
//    }
//    return nil;
//}
//
//+ (void)registerBaiduMap {
//    // 如果要关注网络及授权验证事件，请设定     generalDelegate参数
//    BOOL ret = [[DeviceDataInfo shareDataInfo].mapManager start:BauduKey  generalDelegate:nil];
//    if (!ret) {
//        NSLog(@"manager start failed!");
//    } else {
//        NSLog(@"百度地图初始化成功!!!!!!!!!!!");
//        // 获取位置
//        [[DeviceDataInfo shareDataInfo].locService startUserLocationService];
//
//        // 检测用户是否拒绝定位
//        if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
//        {
//            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"打开[定位服务]来允许太平惠汇确定您的位置" message:@"请在系统设置中开启定位服务(设置>隐私>定位服务>开启)" delegate:[DeviceDataInfo shareDataInfo] cancelButtonTitle:@"取消" otherButtonTitles:@"设置" , nil];
//            alertView.tag = 1;
//            [alertView show];
//        }
//    }
//}
//
//+ (NSString *)latitude {
//    return [DeviceDataInfo shareDataInfo].latitude;
//}
//
//+ (NSString *)longitude {
//    return [DeviceDataInfo shareDataInfo].longitude;
//}
//
//#pragma mark - BMKLocationServiceDelegate
//- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
//    NSString *longitudeNow = [NSString stringWithFormat:@"%f", userLocation.location.coordinate.longitude];
//    NSString *latitudeNow = [NSString stringWithFormat:@"%f", userLocation.location.coordinate.latitude];
//
//    NSString *longitude = [[NSString alloc] init];
//    if ([longitudeNow floatValue] == 0) {
//        longitude = [[DeviceDataInfo shareDataInfo] longitude];
//    } else {
//        longitude = longitudeNow;
//    }
//    NSString *latitude = [[NSString alloc] init];
//    if ([latitudeNow floatValue] == 0) {
//        latitude = [[DeviceDataInfo shareDataInfo] latitude];
//    } else {
//        latitude = latitudeNow;
//    }
//
//    // 内存中保存钥匙串
//    [DeviceDataInfo shareDataInfo].latitude = latitude;
//    [DeviceDataInfo shareDataInfo].longitude = longitude;
//
//    // 将获取到的经纬度保存在钥匙串中
//    [PublickeyChain saveKeyChain:@{
//                                   @"longitude": longitude,
//                                   @"latitude": latitude
//                                   }
//                   DictionaryKey:UserLocationKey KeyChainKey:KeyChainAppKey];
//
//    NSLog(@"经度:%f,维度%f",userLocation.location.coordinate.longitude,userLocation.location.coordinate.latitude);
//    // 获取经纬度,停止定位
//    [self.locService stopUserLocationService];
//}
//
//
//- (void)reachableViaWWAN {
//    NSArray *typeStrings2G = @[CTRadioAccessTechnologyEdge,
//                               CTRadioAccessTechnologyGPRS,
//                               CTRadioAccessTechnologyCDMA1x];
//
//    NSArray *typeStrings3G = @[CTRadioAccessTechnologyHSDPA,
//                               CTRadioAccessTechnologyWCDMA,
//                               CTRadioAccessTechnologyHSUPA,
//                               CTRadioAccessTechnologyCDMAEVDORev0,
//                               CTRadioAccessTechnologyCDMAEVDORevA,
//                               CTRadioAccessTechnologyCDMAEVDORevB,
//                               CTRadioAccessTechnologyeHRPD];
//
//    NSArray *typeStrings4G = @[CTRadioAccessTechnologyLTE];
//
//    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
//        CTTelephonyNetworkInfo *teleInfo= [[CTTelephonyNetworkInfo alloc] init];
//        NSString *accessString = teleInfo.currentRadioAccessTechnology;
//        if ([typeStrings4G containsObject:accessString]) {
//            _status = YWNetWorkStatusWWAN4G;
//        } else if ([typeStrings3G containsObject:accessString]) {
//            _status = YWNetWorkStatusWWAN3G;
//        } else if ([typeStrings2G containsObject:accessString]) {
//            _status = YWNetWorkStatusWWAN2G;
//        } else {
//            _status = YWNetWorkStatusUnknown;
//        }
//    }
//}
//
//#pragma mark - UIAlertViewDelegate
//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//    if (alertView.tag == 1) {
//        if (buttonIndex == 1) {
//            //跳转到定位权限页面
//            NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
//            if( [[UIApplication sharedApplication]canOpenURL:url] ) {
//                [[UIApplication sharedApplication] openURL:url];
//            }
//        }
//    }
//}
//
//+ (void)saveEventToCalendarWithTitle:(NSString *)title remark:(NSString *)remark timeStamp:(NSString *)timeStamp {
//    if ([[DeviceDataInfo shareDataInfo].eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
//        [[DeviceDataInfo shareDataInfo].eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
//            dispatch_async(dispatch_get_main_queue(), ^{
//                if (error) {
//                    NSLog(@"%@",error);
//                } else if (!granted) {
//                    //被用户拒绝，不允许访问日历
//                    jxt_showAlertTwoButton(nil, @"系统提醒功能未开启，只能短信提醒，无法加入日历提醒，去开启吧~", @"取消", nil, @"确定", ^(NSInteger buttonIndex) {
//                        NSLog(@"跳转开启权限");
//                        NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
//                        if([[UIApplication sharedApplication] canOpenURL:url]) {
//                            [[UIApplication sharedApplication] openURL:url];
//                        }
//                    });
//                } else {
//                    //事件保存到日历
//                    EKEvent *event = [EKEvent eventWithEventStore:[DeviceDataInfo shareDataInfo].eventStore];
//                    event.allDay = NO;;
//                    event.title = title;
//                    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//                    [dateFormatter setAMSymbol:@"AM"];
//                    [dateFormatter setPMSymbol:@"PM"];
//                    [dateFormatter setDateFormat:@"yyyy/MM/dd hh:mmaaa"];
//                    NSInteger time = (NSInteger)([timeStamp floatValue] / 1000.0);
//                    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
//                    // 开始时间为活动时间前一小时
//                    event.startDate = date;
//                    // 结束时间为活动时间
//                    event.endDate = date;
//                    // 提醒时间为开始时间
//                    [event addAlarm:[EKAlarm alarmWithRelativeOffset:-60.0 * 60.0]];
//
//                    //事件内容备注
//                    if (remark.hasValue) {
//                        event.notes = remark;
//                    }
//                    [event setCalendar:[[DeviceDataInfo shareDataInfo].eventStore defaultCalendarForNewEvents]];
//                    NSError *err;
//                    [[DeviceDataInfo shareDataInfo].eventStore saveEvent:event span:EKSpanThisEvent error:&err];
//                    if (!error) {
//                        NSLog(@"保存成功");
//                    }
//                }
//            });
//        }];
//    }
//
//}


@end
